package com.hcxdi.base.presenter;

import com.hcxdi.api.bean.base.Rsp;
import com.hcxdi.base.contract.BaseContract;

import java.net.SocketException;

import rx.Subscriber;

/**
 * Created by jiajiabao on 16-11-14.
 */

public abstract   class RxSubscriber<T extends Rsp,P extends RxPresenter<? extends BaseContract.BaseView>> extends Subscriber<T> {
    P mRxPresenter;

    public RxSubscriber(P presenter) {
        this.mRxPresenter = presenter;
    }

    public RxSubscriber(Subscriber<?> subscriber, P presenter) {
        super(subscriber);
        this.mRxPresenter = presenter;
    }

    public RxSubscriber(Subscriber<?> subscriber, boolean shareSubscriptions, P presenter) {
        super(subscriber, shareSubscriptions);
        this.mRxPresenter = presenter;
    }

    @Override
    public void onError(Throwable e) {
        mRxPresenter.getView().onHide();
        mRxPresenter.removeSubscription(this);
        if (e instanceof SocketException){

        }
    }

    @Override
    public void onStart() {
        super.onStart();
        mRxPresenter.getView().onShow();
        mRxPresenter.addSubscription(this);
    }

    @Override
    public void onCompleted() {
        mRxPresenter.removeSubscription(this);
        mRxPresenter.getView().onHide();
    }

    @Override
    public void onNext(T t) {
        mRxPresenter.getView().onHide();
        mRxPresenter.removeSubscription(this);
        onSuccess(t);
    }

    public abstract void onSuccess(T t);

}
